import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.cluster.hierarchy as sch
from scipy.cluster.hierarchy import dendrogram, set_link_color_palette

# 显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
df = pd.read_csv("F:/成信大/多元统计分析/test3-1.csv")
data = df.columns[1: 7]
# 存放元素
u = df[data]
# 获取第一列数据，便于最后生成图的纵坐标下标从1开始而不是0
x = df.columns[0]
index = np.array(df[x])
'''
具体参考官方文档：
https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html
'''
# 聚类方法：最远邻元素；测量区间：欧式距离
# 聚类方法：最近邻元素；测量区间：明可夫斯基
result = sch.linkage(u, method='single', metric='minkowski', optimal_ordering=True)
dn = dendrogram(result, orientation='right', labels=index)
plt.show()
